package com.pet.server.ext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pet.server.ext.MyCookie;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.UserInfo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Result {

    public String cookie, contentType,redirect,type;
    public JSONObject ret;
    private byte[] bytes;
    private ApiError error;
    private List<MyCookie> cookieList = new ArrayList<>();
    
    
    public boolean hasError(){
    	return null!=error;
    }
    public Map<String, Object> map=new HashMap<String, Object>();
    
    
    public Result(byte[] bytes, String contentType) {
    	this.ret = null;
        this.bytes = bytes;
        this.contentType = contentType;
    }
   
    
    public Result() {
    	this.ret = new JSONObject();
    	this.ret.put("code", 200);
        this.cookie = null;
    }
    
    public Result(JSONObject jsonObj, String cookie) {
        this.ret = jsonObj;
        this.cookie = cookie;
    }

    public Result(ApiError.Type errorType, String cookie) {
        this(errorType.toException(), cookie);
    }

    public Result(ApiError error, String cookie) {
        this(error.toJSONObject(), cookie);
        this.error=error;
    }
    
    public Result(JSONObject jsonObj) {
        this(jsonObj, null);
    }
    
    public void setRequestId(long requestId) {
    	ret.put("request_id", requestId);
    }
    
    public void put(Object key,Object value){
    	ret.put(key, value);
    }
    public void setRet(JSONObject obj){
    	obj.put("code", 200);
    	this.ret = obj;
    }
    public void setRet(JSONObject obj,String cookie){
    	obj.put("code", 200);
    	this.ret = obj;
    	this.cookie=cookie;
    }
    public String getResponseString() {
        return ret == null ? null : ret.toString();
    }
    
    public String getCookie() {
        return cookie;
    }
    
    public byte[] getResponseBytes() {
        return bytes;
    }
    
    public String getContentType() {
        return contentType;
    }
	public void setRedirect(String redirect,String type) {
		this.redirect = redirect;
		this.type=type;
	}
	public void setAttr(String key,Object value){
    	map.put(key, value);
    }
	public void setUserInfo(UserInfo u){
		JSONObject o = new JSONObject();
		o.accumulate("nick_name",u.user_name);
		o.accumulate("user_id", u.user_id);
		o.accumulate("ico_path", u.ico_path);
		ret.accumulate("user", o);
	}
	
	public void setPage(Page page,String ...names){
		
		put("data", BeanToJson.listTojson(page.getList(),names));
		put("totalCount",page.totalCount);
		put("startIndex",page.startIndex);
		put("itemCount",page.itemCount);
		//put("page", o);
	}
	
    public void setPage(Page page,JSONArray array){
    	
		put("data", array);
		put("totalCount",page.totalCount);
		put("startIndex",page.startIndex);
		put("itemCount",page.itemCount);
		//put("page", o);
	}
    public void setPage4array(Page page,JSONArray array){
    	JSONObject o = new JSONObject();
    	o.put("list", array);
    	o.put("totalCount",page.totalCount);
		o.put("startIndex",page.startIndex);
		o.put("itemCount",page.itemCount);
		put("data",o);
    }    
	public void setPage(JSONObject o ,Page page,String ...names){
		o.put("list", BeanToJson.listTojson(page.getList(),names));
		o.put("totalCount",page.totalCount);
		o.put("startIndex",page.startIndex);
		o.put("itemCount",page.itemCount);
	}
	
	public void addCookie(MyCookie cookie){
		cookieList.add(cookie);
	}
	public void addCookie(String cookie){
		this.cookie=cookie;
	}
	public List<MyCookie> getCookieList(){
		return this.cookieList;
	}
	
}
